package com.aaa.moneymanage.services.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.entity.*;
import com.aaa.moneymanage.dao.*;
import com.aaa.moneymanage.services.CareHistoryServices;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：XiaoxXuan
 * @date ：Created in 2023/3/24 19:44
 * @description：
 * @modified By：
 */
@Service
public class CareHistoryServicesImpl implements CareHistoryServices {

    @Resource
    private CareHistoryDao careHistoryDao;

    @Resource
    private CareOrderDao careOrderDao;

    @Resource
    private CareOrderItemDao careOrderItemDao;

    @Resource
    private MoneyinDao moneyinDao;

    @Resource
    private OrderChangeItemDao orderChangeItemDao;

    /** * @create by: XiaoXuan
     * @description: 处方收费业务逻辑
     * @create time: 2023/3/30 17:02
     * * @param regId 
     * @return com.aaa.vo.Result 
     */
    @Override
    public Result getOneByRegId(String regId) {
        //根据regId查询对应的医师和患者
        QueryWrapper<CareHistory> queryWrap = new QueryWrapper<>();
        queryWrap.eq("reg_id", regId);
        CareHistory careHistory1 = careHistoryDao.selectOne(queryWrap);
        if (careHistory1 == null) {
            return new Result<Map<String, Object>>(500, "查询数据为空", null);
        }
        //获取ch_id关联his_care_order表
        String chId = careHistory1.getChId();
        QueryWrapper<CareOrder> query = new QueryWrapper<>();
        query.eq("ch_id", chId);
        List<CareOrder> careOrders = careOrderDao.selectList(query);
        List<Map<String, Object>> list = new ArrayList<>();
        if(careOrders.size()>0) {
            for (CareOrder careOrder : careOrders) {
                Map<String, Object> map1 = BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("co_id", careOrder.getCoId());
                queryWrapper.eq("status", 0);
                List<CareOrderItem> careOrderItem = careOrderItemDao.selectList(queryWrapper);
                //根据order表关联order_item表
                //计算出订单总金额
                if (careOrderItem.size() != 0) {
                    Double sum = careOrderItem.stream().
                            map(item -> item.getAmount().doubleValue()).reduce((a, b) -> a + b).get();
                    map1.put("careOrderItem", careOrderItem);
                    map1.put("sum", sum);
                    list.add(map1);
                }
            }
        }
        if(list.isEmpty()){
            return new Result<>(402, "请输入未支付的订单ID", null);
        }else{
            //把数据存放在map中返回
            Map<String, Object> map = new HashMap<>();
            map.put("careHistory", careHistory1);
            map.put("list",list);
            return new Result<>(200, "查询处方收费成功", map);
        }
    }

    /** * @create by: XiaoXuan
     * @description: 处方退费业务逻辑 
     * @create time: 2023/3/30 17:02
     * * @param regId 
     * @return com.aaa.vo.Result 
     */
    @Override
    public Result BackFeeByRegId(String regId) {
        //根据regId查询对应的医师和患者
        QueryWrapper<CareHistory> queryWrap = new QueryWrapper<>();
        queryWrap.eq("reg_id", regId);
        CareHistory careHistory1 = careHistoryDao.selectOne(queryWrap);
        if (careHistory1 == null) {
            return new Result<Map<String, Object>>(500, "查询数据为空", null);
        }
        //获取ch_id关联his_care_order表
        String chId = careHistory1.getChId();
        QueryWrapper<CareOrder> query = new QueryWrapper<>();
        query.eq("ch_id", chId);
        List<CareOrder> careOrders = careOrderDao.selectList(query);
        List<Map<String, Object>> list = new ArrayList<>();
        if(careOrders.size()>0) {
            for (CareOrder careOrder : careOrders) {
                Map<String, Object> map1 = BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("co_id", careOrder.getCoId());
                queryWrapper.eq("status", 1);
                //根据order表关联order_item表
                List<CareOrderItem> careOrderItem = careOrderItemDao.selectList(queryWrapper);
                //计算出订单总金额
                if (careOrderItem.size() != 0) {
                    Double sum = careOrderItem.stream().
                            map(item -> item.getAmount().doubleValue()).reduce((a, b) -> a + b).get();
                    map1.put("careOrderItem", careOrderItem);
                    map1.put("sum", sum);
                    list.add(map1);
                }
            }
        }
        if(list.isEmpty()){
            return new Result<>(402, "请输入未支付的订单ID", null);
        }else{
            //把数据存放在map中返回
            Map<String, Object> map = new HashMap<>();
            map.put("careHistory", careHistory1);
            map.put("list",list);
            return new Result<>(200, "查询处方收费成功", map);
        }
    }

    /** * @create by: XiaoXuan
     * @description: 处方发药业务逻辑 
     * @create time: 2023/3/30 17:02
     * * @param regId 
     * @return com.aaa.vo.Result 
     */
    @Override
    public Result dispensingByRegId(String regId) {
        //根据regId查询对应的医师和患者
        QueryWrapper<CareHistory> queryWrap = new QueryWrapper<>();
        queryWrap.eq("reg_id", regId);
        CareHistory careHistory1 = careHistoryDao.selectOne(queryWrap);
        if (careHistory1 == null) {
            return new Result<Map<String, Object>>(500, "查询数据为空", null);
        }
        //获取ch_id关联his_care_order表
        String chId = careHistory1.getChId();
        QueryWrapper<CareOrder> query = new QueryWrapper<>();
        query.eq("ch_id", chId);
        List<CareOrder> careOrders = careOrderDao.selectList(query);
        List<Map<String, Object>> list = new ArrayList<>();
        if(careOrders.size()>0) {
            for (CareOrder careOrder : careOrders) {
                Map<String, Object> map1 = BeanUtil.beanToMap(careOrder);
                QueryWrapper<CareOrderItem> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("co_id", careOrder.getCoId());
                queryWrapper.eq("item_type", 0);
                queryWrapper.eq("status", 1);
                //根据order表关联order_item表
                List<CareOrderItem> careOrderItem = careOrderItemDao.selectList(queryWrapper);
                //计算出订单总金额
                if (careOrderItem.size() != 0) {
                    Double sum = careOrderItem.stream().
                            map(item -> item.getAmount().doubleValue()).reduce((a, b) -> a + b).get();
                    map1.put("careOrderItem", careOrderItem);
                    map1.put("sum", sum);
                    list.add(map1);
                }
            }
        }
        if(list.isEmpty()){
            return new Result<>(402, "请输入未支付的订单ID", null);
        }else{
            //把数据存放在map中返回
            Map<String, Object> map = new HashMap<>();
            map.put("careHistory", careHistory1);
            map.put("list",list);
            return new Result<>(200, "查询处方收费成功", map);
        }
    }

    /** * @create by: XiaoXuan
     * @description: 获得CareHistory实体类通过regId 
     * @create time: 2023/4/7 20:08
     * * @param regId 
     * @return com.aaa.entity.CareHistory 
     */
    @Override
    public CareHistory getCareHistoryByRegId(String regId) {
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("reg_id",regId);
        return careHistoryDao.selectOne(wrapper);
    }

    /** * @create by: XiaoXuan
     * @description: 获得CareOrderItem通过regId 
     * @create time: 2023/4/7 20:08
     * * @param regId 
     * @return java.util.List<com.aaa.entity.CareOrderItem> 
     */
    @Override
    public List<CareOrderItem> getCareOrderItemByRegId(String regId) {
        return careHistoryDao.getOtherByRegId(regId);
    }

    /** * @create by: XiaoXuan
     * @description: 修改药方表中的状态 （his_care_order_item）
     * @create time: 2023/4/7 20:09
     * * @param itemId
     * @param status 
     * @return boolean 
     */
    @Override
    public boolean updateTheStatus(String itemId ,String status) {

        UpdateWrapper<CareOrderItem> wrapper = new UpdateWrapper<>();

        wrapper.eq("item_id",itemId).set("status",status);

        int update = careOrderItemDao.update(null, wrapper);

        if (update>1){
            return true;
        }
        return false;
    }

    /** * @create by: XiaoXuan
     * @description: 修改收费列表里面状态 
     * @create time: 2023/4/7 20:10
     * * @param itemId
     * @param status 
     * @return boolean 
     */
    @Override
    public boolean updateOrderChargeItemStatus(String itemId, String status) {

        UpdateWrapper<OrderChargeItem> wrapper = new UpdateWrapper<>();

        wrapper.eq("item_id",itemId).set("status",status);

        int update = orderChangeItemDao.update(null, wrapper);

        if (update>1){
            return true;
        }
        return false;
    }

    /** * @create by: XiaoXuan
     * @description: 修改收费列表里面状态通过regId 
     * @create time: 2023/4/7 20:10
     * * @param regId 
     * @return java.lang.String 
     */
    @Transactional
    @Override
    public String updateOrderChargeItemStatusByRegId(String regId) {
        QueryWrapper<OrderCharge> wrapper = new QueryWrapper<>();
        wrapper.eq("reg_id",regId);
        OrderCharge orderCharge = moneyinDao.selectOne(wrapper);

        String orderId = orderCharge.getOrderId();

        QueryWrapper<OrderChargeItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        List<OrderChargeItem> orderChargeItems = orderChangeItemDao.selectList(queryWrapper);

        for (OrderChargeItem orderChargeItem : orderChargeItems) {
            UpdateWrapper<OrderChargeItem> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("item_id",orderChargeItem.getItemId()).set("status","2");
            orderChangeItemDao.update(null, updateWrapper);
        }

        return orderId;
    }


}